package com.steel.yfmall.cloud.assistant.common.impl;


import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.bean.AssistantConstants;
import com.steel.yfmall.cloud.assistant.dto.para.user.ParaSaleAreaBean;
import com.steel.yfmall.cloud.assistant.dto.para.user.ParaSaleGroupInfoBean;
import com.steel.yfmall.cloud.assistant.po.user.StaffSaleArea;
import com.steel.yfmall.cloud.assistant.po.user.StaffSaleGroupInfo;
import com.steel.yfmall.cloud.assistant.user.StaffSaleCodeService;
import com.steel.yfmall.cloud.common.api.PlatformCommonPublicInfoService;
import com.steel.yfmall.cloud.common.bean.param.APIParaDepotInfoBean;
import com.steel.yfmall.cloud.common.bean.param.APIParaSystemSalesAreaBean;
import com.steel.yfmall.cloud.common.bean.param.APIParamMallConfigBean;
import com.steel.yfmall.cloud.common.bean.param.APIParamOrgChannelInfoBean;
import com.steel.yfmall.cloud.common.bean.result.APIResultDepotInfoBean;
import com.steel.yfmall.cloud.common.bean.result.APIResultMallConfigBean;
import com.steel.yfmall.cloud.common.bean.result.APIResultOrgChannelInfoBean;
import com.steel.yfmall.cloud.common.bean.result.APIResultSystemSalesAreaBean;
import com.steel.yfmall.cloud.components.cache.RedisClient;
import com.steel.yfmall.cloud.components.cache.SerializableUtils;
import com.steel.yfmall.cloud.components.utils.base.APIListBean;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.Pages;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class AuxiliaryDataRefresher {

    static private Map<String, APIResultOrgChannelInfoBean> stringAPIResultOrgChannelInfoBeanMap = new HashMap<>();

    /**
     * 定价区域
     */
    static private Map<String, APIResultSystemSalesAreaBean> stringAPIResultSystemSalesAreaBeanHashMap = new HashMap<>();

    /**
     * 仓库信息
     */
    static private Map<String, APIResultDepotInfoBean> apiResultDepotInfoBeanMap = new HashMap<>();

    /**
     * 销售区域
     */
    static private Map<String, StaffSaleArea> apiResultSaleAreaBeanMap = new HashMap<>();

    /**
     * 销售组信息
     */
    static private Map<String, StaffSaleGroupInfo> apiResultSaleGroupInfoBeanMap = new HashMap<>();

    /**
     * 初始化,搜索运行店铺
     * <p>
     * void
     */
    public void initChannelInfo() {
        try {
            if (log.isDebugEnabled()) {
                log.debug("初始化站点渠道信息");
            }
			APIParamOrgChannelInfoBean paraChannelInfoBean=new APIParamOrgChannelInfoBean();
			paraChannelInfoBean.setSiteCode(AssistantConstants.DEFALUT_SITECODE);
            APIMsgBean<Pages<APIResultOrgChannelInfoBean>> apiMsgBean=platformCommonPublicInfoService.getOrgChannelInfoListByPara(paraChannelInfoBean);
            if(apiMsgBean.getData()==null|| CollectionUtils.isEmpty(apiMsgBean.getData().getList())){
                log.error("初始化站点渠道信息失败,渠道返回数据为空:"+ JSON.toJSONString(apiMsgBean));
                return;
            }
            List<APIResultOrgChannelInfoBean> resultOrgChannelInfoBeanList=apiMsgBean.getData().getList();
            for (APIResultOrgChannelInfoBean itemChannelInfoBean:resultOrgChannelInfoBeanList) {
                 stringAPIResultOrgChannelInfoBeanMap.put(itemChannelInfoBean.getChannelCode(),itemChannelInfoBean);
            }
        } catch (Exception ex) {
            log.error("function initChannelInfo", ex);
        }
    }

    /**
     * @return com.yfmall.cloud.share.bean.result.APIResultOrgChannelInfoBean    返回类型
     * @Title: getOrgChannelInfoBean
     * @Description: (获取渠道信息)
     * @Param channelCode 参数说明
     * @author TQ
     * @date 2019/12/18 11:45
     */
     public APIResultOrgChannelInfoBean getOrgChannelInfoBean(String channelCode) {
         if(stringAPIResultOrgChannelInfoBeanMap.isEmpty()){
             initChannelInfo();
         }
        if (stringAPIResultOrgChannelInfoBeanMap != null && !stringAPIResultOrgChannelInfoBeanMap.isEmpty()) {
            return stringAPIResultOrgChannelInfoBeanMap.get(channelCode);
        }
        return null;
    }


    /**
     * 获取定价区域信息
     * @Title: getAPIParaSystemSalesAreaBean
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param salesAreaCode 参数说明
     * @return com.steel.yfmall.cloud.common.bean.result.APIResultSystemSalesAreaBean    返回类型
     * @author yuchaohua
     * @date 2021/1/21 17:28
     */
    public APIResultSystemSalesAreaBean getAPIParaSystemSalesAreaBean(String salesAreaCode) {
        if(StringUtils.isBlank(salesAreaCode)){
            return null;
        }
        if(stringAPIResultSystemSalesAreaBeanHashMap.isEmpty() || !stringAPIResultSystemSalesAreaBeanHashMap.containsKey(salesAreaCode)){
            initPriceSaleInfo();
        }
        if (stringAPIResultSystemSalesAreaBeanHashMap != null && !stringAPIResultSystemSalesAreaBeanHashMap.isEmpty()) {
            return stringAPIResultSystemSalesAreaBeanHashMap.get(salesAreaCode);
        }
        return null;
    }

    /**
     * 初始化站点定价区域信息
     */
    public void initPriceSaleInfo() {
        try {
            if (log.isDebugEnabled()) {
                log.debug("初始化站点定价区域信息");
            }
            APIParaSystemSalesAreaBean paraChannelInfoBean=new APIParaSystemSalesAreaBean();
            paraChannelInfoBean.setSiteCode(AssistantConstants.DEFALUT_SITECODE);
            APIListBean<APIResultSystemSalesAreaBean> apiMsgBean=platformCommonPublicInfoService.getSystemSalesAreaBeanListByPara(paraChannelInfoBean);
            if(CollectionUtils.isEmpty(apiMsgBean.getData())){
                log.error("初始化站点定价区域信息,返回数据为空:"+ JSON.toJSONString(apiMsgBean));
                return;
            }
            List<APIResultSystemSalesAreaBean> resultOrgChannelInfoBeanList=apiMsgBean.getData();
            for (APIResultSystemSalesAreaBean itemChannelInfoBean:resultOrgChannelInfoBeanList) {
                stringAPIResultSystemSalesAreaBeanHashMap.put(itemChannelInfoBean.getSalesAreaCode(),itemChannelInfoBean);
            }
        } catch (Exception ex) {
            log.error("function initChannelInfo", ex);
        }
    }

    /**
     * 初始化站点下配置文件
     * @Title: initSiteConfigInfoMap
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param  参数说明
     * @return void    返回类型
     * @author yuchaohua
     * @date 2020/1/8 21:10
     */
    public void initSiteConfigInfoMap() {
        APIParamMallConfigBean paraBean = new APIParamMallConfigBean();
        paraBean.setSiteCode(AssistantConstants.DEFALUT_SITECODE);
        // 配置状态 1启用
        paraBean.setStatus(1);
        APIListBean<APIResultMallConfigBean> result = platformCommonPublicInfoService.gyfmallConfigInfoListByPara(paraBean);
        if (result != null && CollectionUtils.isNotEmpty(result.getData())) {
            List<APIResultMallConfigBean> list = result.getData();
            Map<String, String> configMap = new HashMap<>();
            for (APIResultMallConfigBean configBean : list) {
                configMap.put(configBean.getConfigKey(), configBean.getConfigValue());
            }
            byte[] bytes = null;
            try {
                bytes = SerializableUtils.getBytesFromCommon(configMap);
            } catch (IOException e) {
                log.error("function initSiteConfigInfoMap", e);
            }
            redisClient.set(AssistantConstants.CONFIG_INFO_MAP, bytes);
        } else {
            redisClient.del(AssistantConstants.CONFIG_INFO_MAP);
        }
    }


    /**
     * 获取仓库信息
     * @Title: getDepotInfoBean
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param depotCode 参数说明
     * @return com.steel.yfmall.cloud.common.bean.result.APIResultDepotInfoBean    返回类型
     * @author yuchaohua
     * @date 2020/12/14 22:54
     */
    public APIResultDepotInfoBean getDepotInfoBean(String depotCode) {
        if(StringUtils.isBlank(depotCode)){
            return null;
        }
        if(apiResultDepotInfoBeanMap.isEmpty() || !apiResultDepotInfoBeanMap.containsKey(depotCode)){
            initDepotBeanInfo();
        }
        if (apiResultDepotInfoBeanMap != null && !apiResultDepotInfoBeanMap.isEmpty()) {
            return apiResultDepotInfoBeanMap.get(depotCode);
        }
        return null;
    }

    /**
     * 初始化仓库信息
     * @Title: initDepotBeanInfo
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param  参数说明
     * @return void    返回类型
     * @author yuchaohua
     * @date 2020/12/14 22:54
     */
    public void initDepotBeanInfo() {
        try {
            if (log.isDebugEnabled()) {
                log.debug("初始化站点仓库信息");
            }
            APIParaDepotInfoBean paraChannelInfoBean = new APIParaDepotInfoBean();
            paraChannelInfoBean.setSiteCode(AssistantConstants.DEFALUT_SITECODE);
            APIMsgBean<HashMap<String, APIResultDepotInfoBean>> apiMsgBean = platformCommonPublicInfoService.getDepotInfoBeanMapByPara(paraChannelInfoBean);
            if (apiMsgBean == null || apiMsgBean.getData() == null || apiMsgBean.getData().size() <= 0) {
                log.error("初始化站点仓库信息,返回数据为空:" + JSON.toJSONString(apiMsgBean));
                return;
            }
            HashMap<String, APIResultDepotInfoBean> resultDepotInfoBeanHashMap = apiMsgBean.getData();
            for (Map.Entry<String, APIResultDepotInfoBean> depot : resultDepotInfoBeanHashMap.entrySet()) {
                try {
                    APIResultDepotInfoBean depotBean = JSON.parseObject(JSON.toJSONString(depot.getValue()), APIResultDepotInfoBean.class);
                    String depotCode = depot.getKey();
                    apiResultDepotInfoBeanMap.put(depotCode, depotBean);
                } catch (Exception e) {
                    log.error("获取仓库信息失败：" + JSON.toJSONString(depot.getValue()), e);
                }
            }
        } catch (Exception ex) {
            log.error("function initDepotBeanInfo", ex);
        }
    }

    /**
     * 获取销售地区
     * @Title: getSaleAreaBean
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param saleAreaCode 参数说明
     * @return com.steel.yfmall.cloud.assistant.po.user.StaffSaleArea    返回类型
     * @author yuchaohua
     * @date 2021/1/21 18:54
     */
    public StaffSaleArea getSaleAreaBean(String saleAreaCode) {
        if(StringUtils.isBlank(saleAreaCode)){
            return null;
        }
        if(apiResultSaleAreaBeanMap.isEmpty() || !apiResultSaleAreaBeanMap.containsKey(saleAreaCode)){
            initStaffSaleArea();
        }
        if (apiResultSaleAreaBeanMap != null && !apiResultSaleAreaBeanMap.isEmpty()) {
            return apiResultSaleAreaBeanMap.get(saleAreaCode);
        }
        return null;
    }

    public void initStaffSaleArea() {
        try {
            if (log.isDebugEnabled()) {
                log.debug("初始化销售区域信息");
            }
            ParaSaleAreaBean paraBean = new ParaSaleAreaBean();
            paraBean.setSiteCode(AssistantConstants.DEFALUT_SITECODE);
            List<StaffSaleArea> resultSaleAreaList = staffSaleCodeService.getSaleAreaListByPara(paraBean);
            if (CollectionUtils.isEmpty(resultSaleAreaList)) {
                log.error("初始化销售区域信息,返回数据为空");
                return;
            }
            for (StaffSaleArea itemBean : resultSaleAreaList) {
                apiResultSaleAreaBeanMap.put(itemBean.getSaleAreaCode(), itemBean);
            }
        } catch (Exception ex) {
            log.error("function initStaffSaleArea", ex);
        }
    }

    /**
     * 获取销售组信息
     * @Title: getSaleGroupInfoBean
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param saleGroupCode 参数说明
     * @return com.steel.yfmall.cloud.assistant.po.user.StaffSaleGroupInfo    返回类型
     * @author yuchaohua
     * @date 2021/1/21 19:01
     */
    public StaffSaleGroupInfo getSaleGroupInfoBean(String saleGroupCode) {
        if(StringUtils.isBlank(saleGroupCode)){
            return null;
        }
        if(apiResultSaleGroupInfoBeanMap.isEmpty() || !apiResultSaleGroupInfoBeanMap.containsKey(saleGroupCode)){
            initSaleGroupInfo();
        }
        if (apiResultSaleGroupInfoBeanMap != null && !apiResultSaleGroupInfoBeanMap.isEmpty()) {
            return apiResultSaleGroupInfoBeanMap.get(saleGroupCode);
        }
        return null;
    }

    /**
     * 初始化销售组
     */
    public void initSaleGroupInfo() {
        try {
            if (log.isDebugEnabled()) {
                log.debug("初始化初始化销售组信息");
            }
            ParaSaleGroupInfoBean paraBean = new ParaSaleGroupInfoBean();
            paraBean.setSiteCode(AssistantConstants.DEFALUT_SITECODE);
            List<StaffSaleGroupInfo> resultSaleAreaList = staffSaleCodeService.getSaleGroupInfoListByPara(paraBean);
            if (CollectionUtils.isEmpty(resultSaleAreaList)) {
                log.error("初始化销售组信息,返回数据为空");
                return;
            }
            for (StaffSaleGroupInfo itemBean : resultSaleAreaList) {
                apiResultSaleGroupInfoBeanMap.put(itemBean.getSaleGroupCode(), itemBean);
            }
        } catch (Exception ex) {
            log.error("function initStaffSaleArea", ex);
        }
    }

    @Resource
    private StaffSaleCodeService staffSaleCodeService;

    @Resource
    private PlatformCommonPublicInfoService platformCommonPublicInfoService;

    @Resource
    public RedisClient redisClient;

}
